@extends('layouts.header')
@section('title','检测设备列表')
@section('content')

    <div class="page-container">
        全选：<input type="checkbox" id="checkAll">
        <a class="btn btn-primary radius mb-10 ml-20" id="submit">确定</a>
        <form id="form" enctype="multipart/form-data">
            @csrf
            <input class="required" type="hidden" name="item_id" value="{{request()->item_id}}">
            <input class="required" type="hidden" name="sample_info_id" value="{{request()->sample_id}}">
            <table class="table table-border table-bordered table-hover table-bg" id="datatable" style="white-space: nowrap;">
                <thead>
                <tr class="text-c">
                    <th></th>
                    <th>序号</th>
                    <th>仪器设备名称</th>
                    <th>设备编号</th>
                    <th>出厂编号</th>
                    <th>规格/型号</th>
                    <th>量程范围</th>
                    <th>准确度/等级</th>
                    <th>溯源方式</th>
                    <th>溯源单位</th>
                    <th>溯源日期</th>
                    <th>有效截止日期至</th>
                    <th>校准周期</th>
                    <th>设备状态</th>
                    <th>负责人</th>
                    <th>生产厂家</th>
                    <th>购入日期</th>
                    <th>设备局域网IP</th>
                    <th>主机局域网IP</th>
                    <th>创建日期</th>
                    <th>备注</th>
                </tr>
                </thead>
                <tbody></tbody>
            </table>
        </form>
    </div>
    </div>
    <script type="text/javascript">
        var table, dataIndex = @json($data),staIds = ','+'{{request()->staIds}}'+',';
        $(function () {
            $.fn.dataTable.ext.errMode = 'throw'; //datatable抛异常的方式，在console中输出，默认以alert的方式弹出
            table = $('#datatable').DataTable({
                paging: false,
                // pageLength: 3,
                // dom: "it" +
                // "< 'row'p>",
                // lengthChange: !1,
                "scrollX": true,
                "deferRender": true, //延迟渲染速度
                "ordering": false, // 是否排序
                ajax: "/{{ config('constant.name_admin') }}/station/index",
                "processing": true, //加载数据时候是否显示进度条
                language: {
                    url: '/admin/lib/datatables/dataTables_Chinese_Lang.json'
                },
                columns: [
                    {
                        data: null, class: "text-c checkBox", render: function (data, type, row, meta) {
                            return "<input type='checkbox' name='id[]' value='" + row.id + "'>";
                        }
                    },
                    {data: null, class: "text-c num"},
                    {data: "name", class: "node-name"},
                    {data: "dev_num", class:'dev_num', defaultContent: '/'},
                    {data: "fac_num", defaultContent: '/'},
                    {data: "spec", defaultContent: '/'},
                    {data: 'ranges', defaultContent: '/'},
                    {data: "accuracy", defaultContent: '/'},
                    {
                        data: "trace_type", class: "text-c", render: function (data, type, row, meta) {
                            return dataIndex.trace_type[data];
                        }
                    },
                    {data: "trace_com", defaultContent: '/'},
                    {data: "trace_date", class: "text-c", defaultContent: '/'},
                    {data: "trace_exp_date", class: "text-c trace_exp_date", defaultContent: '/'},
                    {
                        data: "trace_cycle", class: "text-c", render: function (data, type, row, meta) {
                            return dataIndex.trace_cycle[data];
                        }
                    },
                    {
                        data: "status", class: "text-c", render: function (data, type, row, meta) {
                            return dataIndex.status[data];
                        }
                    },
                    {data: "user", class: "text-c", defaultContent: '/'},
                    {data: "factory", defaultContent: '/'},
                    {data: "buy_date", class: "text-c", defaultContent: '/'},
                    {data: "dev_lan_ip", class: "text-c", defaultContent: '/'},
                    {data: "com_lan_ip", class: "text-c", defaultContent: '/'},
                    {
                        data: "ctime", class: "text-c", defaultContent: '/', render: function (data, type, row, meta) {
                            return data.substring(0, 10);
                        }
                    },
                    {data: "remark", class: "text-c"},
                ],

            });
            table.on('draw', function (a, b) {
                let pageinfo = table.page.info();
                $(a.target.rows).each(function (index) {
                    $(this).find('td.num').text(pageinfo.start + index);
                    $('td').each(function () {
                        if (!$(this).hasClass('checkBox')) {
                            $(this).text() === '' ? $(this).text('/') : '';
                        }else{
                           let id = $(this).find('input[type=checkbox]').val();
                           if(staIds.indexOf(id) != -1){
                               $(this).find('input[type=checkbox]').prop('checked',true);
                           }
                        }
                    });
                })
                table.columns.adjust();
            })
        });

        //确定
        $('#submit').on('click', function () {
            let str = stati_ids = '';
            $('table input[type=checkbox]:checked').each(function () {
                stati_ids += ','+$(this).val();
                str += '<tr>' +
                    '<td>'+$(this).closest('tr').find('td.node-name').text()+'</td>'+
                    '<td>'+$(this).closest('tr').find('td.dev_num').text()+'</td>'+
                    '<td>'+$(this).closest('tr').find('td.trace_exp_date').text()+'</td>'+
                    '</tr>';
            })
            let itmid = $('input[name=item_id]').val();
            let tbodyId = "#tbody_"+itmid;
            parent.$(tbodyId).html(str);
            parent.$('#station_ids_'+itmid).val(stati_ids.substring(1));
            parent.$(tbodyId).closest('form').find('.def-tip').text('以下为修改后的默认设备列表，请及时保存！');
            layer_close();
        })

    </script>

@endsection